<?php
/**
 * Author: DefinitlyEvil
 * Created at: 2020/4/14 23:14
 */

namespace App\Tools;


class ImageCode
{
    /**
     * @param $redis \Redis
     * @param $id string
     * @param $code string
     * @return boolean
     */
    public static function verify($redis, $id, $code) {
        if(!isset($id) or !Pattern::matches('/^[a-z0-9]{40}$/', $id)) return false;
        if(!isset($code) or empty($code) or !Pattern::matches('/^[A-Za-z0-9]{4}$/', $code)) return false;
        $key = 'image-code:' . $id;
        $real = $redis->get($key);
        if($real === false) return false;
        $ok = $code === $real;
        if($ok) $redis->del($key);
        return $ok;
    }
}